package cn.copera.dyy.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.copera.dyy.common.service.BaseService;
import cn.copera.dyy.dao.ExchangeRecordDao;
import cn.copera.dyy.dao.ExchangeRuleDao;
import cn.copera.dyy.dao.PointPraiseSetDao;
import cn.copera.dyy.dao.ShareSetDao;
import cn.copera.dyy.dao.ShopDao;
import cn.copera.dyy.dao.ShopImageDao;
import cn.copera.dyy.dao.TrackDao;
import cn.copera.dyy.entity.ExchangeRecord;
import cn.copera.dyy.entity.ExchangeRule;
import cn.copera.dyy.entity.PointPraiseSet;
import cn.copera.dyy.entity.ShareSet;
import cn.copera.dyy.entity.Shop;
import cn.copera.dyy.entity.ShopImage;
import cn.copera.dyy.entity.Track;

/**
 * 店铺Service
 * 
 */
@Service
@Transactional(readOnly = true)
public class ShopService extends BaseService {

	@Autowired
	private ShopDao shopDao;
	@Autowired
	private TrackDao trackDao;
	@Autowired
	private ExchangeRecordDao exchangeRecordDao;
	@Autowired
	private ExchangeRuleDao exchangeRuleDao;
	@Autowired
	private ShopImageDao shopImageDao;
	@Autowired
	private PointPraiseSetDao pointPraiseSetDao;
	@Autowired
	private ShareSetDao shareSetDao;

	
	@Transactional(readOnly = false)
	public void save(Shop shop) {
		shopDao.clear();
		shopDao.save(shop);
	}
	public List<Shop> findAll(){
		return shopDao.findAll();
	}
	public List<Shop> findById(String id){
		//return shopDao.find("select * from ka_shop where parent_id=?",new Parameter(id));
		return shopDao.find("from Shop where parent.id = '"+id+"'");
	}
	
	@Transactional(readOnly = false)
	public void deleteById(String id){
		shopDao.deleteRealById(id);
	}
	
	public String checkLogin(String account,String password){
		
		Shop shop = shopDao.findExistEntity(new Shop(), "loginName = '"+account+"'");
		if (shop == null) {
			return "用户名不存在";
		}else {
			if (password.equals(shop.getPassword())) {
				return "登录成功";
			}else{
				return "密码输入不正确";
			}
		}
	}
	public Shop findShop(String account){
		
		return shopDao.findExistEntity(new Shop(), "loginName = '"+account+"'");
	}
	public Shop get(String id) {
		return shopDao.get(id);
	}
	public int getUserCount(String username){
		return shopDao.getEntityCount(new Shop(), "loginName='"+username+"'");
	}
	/**
	 * 统计
	 * @param ShopId
	 * @return
	 */
	public Map<String, Object> count(String account){
		Map<String, Object> map = new HashMap<String, Object>();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		//今天留下的足迹
		Integer trackToday = trackDao.getEntityCount(new Track(), "shop.loginName = '"+account+"' and createDate >= '"+dateStr+" 00:00:00' and createDate <= '"+dateStr+" 23:59:59'");
		//今天兑换的礼品
		Integer exchangeToday = exchangeRecordDao.getEntityCount(new ExchangeRecord(), "shop.loginName = '"+account+"' and exchangeDate >= '"+dateStr+" 00:00:00' and exchangeDate <= '"+dateStr+" 23:59:59'");
		List<ExchangeRule> list = exchangeRuleDao.find("from ExchangeRule where shop.loginName = '"+account+"'");
		map.put("exchangeToday", exchangeToday);
		map.put("trackToday", trackToday);
		map.put("ruleList", list);

		return map;
	}
	public ShopImage findShopImg(String shopId){
		List<ShopImage> list = shopImageDao.find("from ShopImage where shop.id = '"+shopId+"' and flag = 'shop_img' order by createDate desc");
	    if (list.isEmpty()) {
			return null;
		}else{
			return list.get(0);
		}
	}
	public List<ShopImage> findSetImg(String shopId){
		List<ShopImage> list = shopImageDao.find("from ShopImage where shop.id = '"+shopId+"' and flag = 'set' order by createDate desc");
	    return list;
	}
	//设置卡点有效期
	@Transactional(readOnly = false)
	public void validDate(String shopId,String validDate){
		shopDao.update("update Shop set validDate = '"+validDate+"' where id = '"+shopId+"'");
	}
	
	//获取店铺点赞和分享
	public Map<String,Object> getPraiseOrShare(String shopId){
		Map<String, Object> map = new HashMap<String, Object>();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		//今天留下的足迹
		Integer trackToday = trackDao.getEntityCount(new Track(), "shop.id = '"+shopId+"' and createDate >= '"+dateStr+" 00:00:00' and createDate <= '"+dateStr+" 23:59:59'");
		//今天兑换的礼品
		Integer exchangeToday = exchangeRecordDao.getEntityCount(new ExchangeRecord(), "shop.id = '"+shopId+"' and exchangeDate >= '"+dateStr+" 00:00:00' and exchangeDate <= '"+dateStr+" 23:59:59'");
		
		Shop shop = shopDao.getByHql("from Shop where id = '"+shopId+"'");
		map.put("exchangeToday", exchangeToday);
		map.put("trackToday", trackToday);
		map.put("praiseCount", shop.getTotal_count());
		map.put("shareCount", shop.getTotal_count1());
		return map;
	}
}
